<?php
require_once 'config.php';

if (isLoggedIn()) {
    header('Location: backend/php/dashboard.php');
    exit;
}

// Login logic (kept from original file)
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = sanitizeInput($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';

    if (empty($username) || empty($password)) {
        $error = 'Please enter both username and password';
    } else {
        $conn = getDBConnection();
        $stmt = $conn->prepare("SELECT * FROM users WHERE username = ?");
        $stmt->execute([$username]);
        $user = $stmt->fetch();

        if ($user && verifyPassword($password, $user['password'])) {
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['username'] = $user['username'];
            $_SESSION['role'] = $user['role'];
            $_SESSION['full_name'] = $user['full_name'];

            header('Location: backend/php/dashboard.php');
            exit;
        } else {
            $error = 'Invalid username or password';
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo htmlspecialchars(getSystemSetting('app_name', 'PAB Inventory Count System')); ?> - Home</title>
    <!-- Load Tailwind CSS -->
    <script src="https://cdn.tailwindcss.com"></script>
    <!-- Load Font Awesome for icons -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        /* Custom styles for background and effects */
        .hero-bg {
            background-color: #1f2937; /* Dark background */
            background-image: radial-gradient(at 20% 60%, #4f46e5 0%, transparent 50%),
                              radial-gradient(at 80% 20%, #8b5cf6 0%, transparent 50%);
            position: relative;
            overflow: hidden;
        }

        /* Utility classes for buttons not covered by default Tailwind (like large primary) */
        .btn-primary-lg {
            @apply px-8 py-3 text-lg font-semibold rounded-xl transition duration-300 ease-in-out transform hover:scale-[1.02] shadow-lg;
        }

        .btn-secondary-lg {
            @apply px-8 py-3 text-lg font-semibold rounded-xl border-2 transition duration-300 ease-in-out transform hover:bg-white/10 hover:shadow-xl;
        }

        /* Custom styles for the login modal */
        .login-modal {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.7);
            display: none;
            align-items: center;
            justify-content: center;
            z-index: 50;
            backdrop-filter: blur(8px);
        }

        .login-modal.active {
            display: flex;
        }

        .login-modal-content {
            @apply bg-white p-8 rounded-2xl shadow-2xl max-w-sm w-11/12 relative;
            animation: slideUp 0.3s ease-out;
        }

        @keyframes slideUp {
            from {
                opacity: 0;
                transform: translateY(50px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
    </style>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        'primary-500': '#6366f1',
                        'primary-600': '#4f46e5',
                    },
                    fontFamily: {
                        sans: ['Inter', 'sans-serif'],
                    },
                    boxShadow: {
                        '3xl': '0 35px 60px -15px rgba(0, 0, 0, 0.3)',
                    }
                }
            }
        }
    </script>
</head>
<body class="font-sans bg-gray-50 text-gray-800">

    <!-- Navigation -->
    <nav class="sticky top-0 z-20 bg-white/95 backdrop-blur-sm shadow-md transition-all duration-300">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex justify-between items-center h-16">
                <!-- Brand/Logo -->
                <div class="flex-shrink-0 flex items-center">
                    <?php $logo_url = getSystemSetting('logo_url'); ?>
                    <?php if ($logo_url): ?>
                        <img src="<?php echo htmlspecialchars($logo_url); ?>" alt="Logo" class="h-8 w-auto mr-3 rounded-full shadow-md">
                    <?php else: ?>
                        <i class="fas fa-warehouse text-primary-600 text-2xl mr-2"></i>
                    <?php endif; ?>
                    <span class="text-xl font-bold text-gray-900"><?php echo htmlspecialchars(getSystemSetting('app_name', 'Inventory System')); ?></span>
                </div>

                <!-- Nav Links (Desktop) -->
                <div class="hidden md:flex space-x-4">
                    <a href="#" class="text-gray-600 hover:text-primary-600 px-3 py-2 rounded-md text-sm font-medium">Home</a>
                    <a href="#features" class="text-gray-600 hover:text-primary-600 px-3 py-2 rounded-md text-sm font-medium">Features</a>
                    <a href="#contact" class="text-gray-600 hover:text-primary-600 px-3 py-2 rounded-md text-sm font-medium">Contact</a>
                </div>

                <!-- Auth Buttons -->
                <div class="flex items-center space-x-3">
                    <button class="text-primary-600 font-medium hover:text-primary-700 transition duration-150 ease-in-out px-3 py-2 rounded-md text-sm" onclick="openLoginModal()">
                        <i class="fas fa-sign-in-alt mr-1"></i> Login
                    </button>
                    <a href="register.php" class="bg-primary-600 text-white px-4 py-2 rounded-xl text-sm font-medium shadow-md hover:bg-primary-700 transition duration-150 ease-in-out">
                        <i class="fas fa-user-plus mr-1"></i> Register
                    </a>
                </div>
            </div>
        </div>
    </nav>

    <!-- Hero Section -->
    <section class="hero-bg text-white pt-24 pb-32">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 text-center">
            <div class="max-w-4xl mx-auto">
                <div class="inline-flex items-center justify-center p-4 mb-6 bg-primary-500/30 rounded-full shadow-lg border border-primary-500/50">
                    <i class="fas fa-warehouse text-4xl text-white"></i>
                </div>
                <h1 class="text-4xl sm:text-6xl font-extrabold mb-4 leading-tight">
                    <span class="block"><?php echo htmlspecialchars(getSystemSetting('app_name', 'PAB Inventory Count System')); ?></span>
                    <span class="block bg-clip-text text-transparent bg-gradient-to-r from-indigo-300 to-purple-400">Digital Inventory Management</span>
                </h1>
                <p class="text-xl text-gray-300 mb-10 max-w-3xl mx-auto">
                    Streamline your inventory counting process with our comprehensive digital solution. Accurate, efficient, and user-friendly management for modern businesses.
                </p>
                <div class="flex justify-center space-x-4">
                    <button class="btn-primary-lg bg-primary-600 text-white hover:bg-primary-700" onclick="openLoginModal()">
                        <i class="fas fa-play mr-2"></i> Get Started Now
                    </button>
                    <a href="#features" class="btn-secondary-lg text-white border-white/50">
                        <i class="fas fa-info-circle mr-2"></i> Explore Features
                    </a>
                </div>
            </div>
        </div>
    </section>

    <!-- Stats Section -->
    <section class="bg-white py-16 -mt-16 relative z-10">
        <div class="max-w-6xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="bg-white p-6 sm:p-10 rounded-2xl shadow-3xl grid grid-cols-2 md:grid-cols-4 gap-8">
                <div class="text-center">
                    <span class="block text-4xl font-extrabold text-primary-600 mb-1">99.9%</span>
                    <span class="text-sm font-medium text-gray-500 uppercase tracking-wider">Accuracy</span>
                </div>
                <div class="text-center">
                    <span class="block text-4xl font-extrabold text-primary-600 mb-1">50%+</span>
                    <span class="text-sm font-medium text-gray-500 uppercase tracking-wider">Time Saved</span>
                </div>
                <div class="text-center">
                    <span class="block text-4xl font-extrabold text-primary-600 mb-1">24/7</span>
                    <span class="text-sm font-medium text-gray-500 uppercase tracking-wider">Availability</span>
                </div>
                <div class="text-center">
                    <span class="block text-4xl font-extrabold text-primary-600 mb-1">100+</span>
                    <span class="text-sm font-medium text-gray-500 uppercase tracking-wider">Customers</span>
                </div>
            </div>
        </div>
    </section>

    <!-- Features Section -->
    <section id="features" class="py-20 bg-gray-50">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="text-center mb-16">
                <h2 class="text-base text-primary-600 font-semibold tracking-wide uppercase mb-2">The Digital Difference</h2>
                <p class="mt-2 text-4xl font-extrabold text-gray-900 sm:text-5xl">
                    Powerful Features to Simplify Your Count
                </p>
            </div>

            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">
                <!-- Feature Card 1 -->
                <div class="bg-white p-8 rounded-2xl shadow-xl hover:shadow-2xl transition duration-300 ease-in-out transform hover:-translate-y-1 border-t-4 border-primary-600/70">
                    <div class="flex items-center justify-center w-16 h-16 mb-6 bg-primary-100 rounded-xl">
                        <i class="fas fa-calculator text-2xl text-primary-600"></i>
                    </div>
                    <h3 class="text-xl font-bold text-gray-900 mb-3">Digital Counting</h3>
                    <p class="text-gray-600 leading-relaxed">
                        Replace error-prone manual sheets with digital forms. Track physical inventory counts with precision and real-time synchronization.
                    </p>
                </div>
                <!-- Feature Card 2 -->
                <div class="bg-white p-8 rounded-2xl shadow-xl hover:shadow-2xl transition duration-300 ease-in-out transform hover:-translate-y-1 border-t-4 border-purple-600/70">
                    <div class="flex items-center justify-center w-16 h-16 mb-6 bg-purple-100 rounded-xl">
                        <i class="fas fa-check-circle text-2xl text-purple-600"></i>
                    </div>
                    <h3 class="text-xl font-bold text-gray-900 mb-3">Quality Assurance</h3>
                    <p class="text-gray-600 leading-relaxed">
                        Implement built-in checking workflows. Independent verification stages prevent costly inventory discrepancies before they occur.
                    </p>
                </div>
                <!-- Feature Card 3 -->
                <div class="bg-white p-8 rounded-2xl shadow-xl hover:shadow-2xl transition duration-300 ease-in-out transform hover:-translate-y-1 border-t-4 border-pink-600/70">
                    <div class="flex items-center justify-center w-16 h-16 mb-6 bg-pink-100 rounded-xl">
                        <i class="fas fa-chart-bar text-2xl text-pink-600"></i>
                    </div>
                    <h3 class="text-xl font-bold text-gray-900 mb-3">Real-time Reports</h3>
                    <p class="text-gray-600 leading-relaxed">
                        Generate comprehensive, up-to-the-minute reports instantly. Track progress, identify variances, and drive data-backed decisions.
                    </p>
                </div>
                <!-- Feature Card 4 -->
                <div class="bg-white p-8 rounded-2xl shadow-xl hover:shadow-2xl transition duration-300 ease-in-out transform hover:-translate-y-1 border-t-4 border-green-600/70">
                    <div class="flex items-center justify-center w-16 h-16 mb-6 bg-green-100 rounded-xl">
                        <i class="fas fa-users text-2xl text-green-600"></i>
                    </div>
                    <h3 class="text-xl font-bold text-gray-900 mb-3">Team Collaboration</h3>
                    <p class="text-gray-600 leading-relaxed">
                        Seamlessly coordinate tasks between multiple counters and checkers. Assign roles, track contributions, and maintain accountability.
                    </p>
                </div>
                <!-- Feature Card 5 -->
                <div class="bg-white p-8 rounded-2xl shadow-xl hover:shadow-2xl transition duration-300 ease-in-out transform hover:-translate-y-1 border-t-4 border-yellow-600/70">
                    <div class="flex items-center justify-center w-16 h-16 mb-6 bg-yellow-100 rounded-xl">
                        <i class="fas fa-mobile-alt text-2xl text-yellow-600"></i>
                    </div>
                    <h3 class="text-xl font-bold text-gray-900 mb-3">Mobile Friendly</h3>
                    <p class="text-gray-600 leading-relaxed">
                        Full accessibility from any modern device. Count inventory using tablets, phones, or desktops thanks to a fully responsive design.
                    </p>
                </div>
                <!-- Feature Card 6 -->
                <div class="bg-white p-8 rounded-2xl shadow-xl hover:shadow-2xl transition duration-300 ease-in-out transform hover:-translate-y-1 border-t-4 border-blue-600/70">
                    <div class="flex items-center justify-center w-16 h-16 mb-6 bg-blue-100 rounded-xl">
                        <i class="fas fa-shield-alt text-2xl text-blue-600"></i>
                    </div>
                    <h3 class="text-xl font-bold text-gray-900 mb-3">Secure & Reliable</h3>
                    <p class="text-gray-600 leading-relaxed">
                        Enterprise-grade security standards with fine-grained, role-based access control to protect your sensitive inventory data.
                    </p>
                </div>
            </div>
        </div>
    </section>

    <!-- CTA Section (Bottom) -->
    <section id="contact" class="py-20 bg-primary-600">
        <div class="max-w-5xl mx-auto px-4 sm:px-6 lg:px-8 text-center text-white">
            <h2 class="text-4xl sm:text-5xl font-extrabold mb-4">
                Ready to Experience <span class="text-indigo-200">Zero Count Errors</span>?
            </h2>
            <p class="text-xl text-indigo-100 mb-10 max-w-3xl mx-auto">
                Join our growing list of satisfied partners and digitally transform your physical inventory process today.
            </p>
            <div class="flex justify-center space-x-4">
                <button class="btn-primary-lg bg-white text-primary-600 hover:bg-gray-100" onclick="openLoginModal()">
                    <i class="fas fa-rocket mr-2"></i> Start Your Trial
                </button>
                <a href="register.php" class="btn-secondary-lg border-white text-white hover:bg-white/10">
                    <i class="fas fa-question-circle mr-2"></i> Register Account
                </a>
            </div>
        </div>
    </section>

    <!-- Footer -->
    <footer class="bg-gray-800 text-white py-10">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 text-center">
            <p class="text-gray-400">&copy; <?php echo date("Y"); ?> <?php echo htmlspecialchars(getSystemSetting('app_name', 'PAB Inventory System')); ?>. All rights reserved.</p>
            <div class="mt-4 flex justify-center space-x-6">
                <a href="#" class="text-gray-400 hover:text-white transition duration-150">Privacy Policy</a>
                <a href="#" class="text-gray-400 hover:text-white transition duration-150">Terms of Service</a>
            </div>
        </div>
    </footer>

    <!-- Login Modal -->
    <div id="loginModal" class="login-modal">
        <div class="login-modal-content">
            <button class="modal-close text-gray-400 hover:text-gray-700 transition duration-150" onclick="closeLoginModal()">&times;</button>
            <div class="text-center mb-6">
                <i class="fas fa-user-circle text-primary-600 text-5xl mb-3"></i>
                <h1 class="text-2xl font-bold text-gray-900 mb-1">Welcome Back</h1>
                <p class="text-gray-500 text-sm">Sign in to access your inventory dashboard</p>
            </div>

            <?php if (isset($error)): ?>
                <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded-xl relative mb-4" role="alert">
                    <span class="block sm:inline"><?php echo $error; ?></span>
                </div>
            <?php endif; ?>

            <form method="POST" class="space-y-4">
                <div>
                    <label for="username" class="block text-sm font-medium text-gray-700 mb-1">
                        <i class="fas fa-user mr-1 text-primary-600"></i> Username
                    </label>
                    <input type="text" id="username" name="username" placeholder="Enter your username" required class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-primary-500 focus:border-primary-500 transition duration-150" value="<?php echo htmlspecialchars($_POST['username'] ?? ''); ?>">
                </div>
                <div>
                    <label for="password" class="block text-sm font-medium text-gray-700 mb-1">
                        <i class="fas fa-lock mr-1 text-primary-600"></i> Password
                    </label>
                    <input type="password" id="password" name="password" placeholder="Enter your password" required class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-primary-500 focus:border-primary-500 transition duration-150">
                </div>
                <button type="submit" class="w-full bg-primary-600 text-white btn-primary-lg focus:ring-4 focus:ring-primary-500/50">
                    <i class="fas fa-sign-in-alt mr-2"></i> Sign In
                </button>
            </form>

            <div class="mt-6 pt-4 border-t border-gray-200">
                <p class="text-center text-sm text-gray-500">
                    Don't have an account? <a href="register.php" class="text-primary-600 font-medium hover:text-primary-700 transition duration-150">Create an account</a>
                </p>
            </div>
        </div>
    </div>

    <script>
        // Javascript for modal functionality
        function openLoginModal() {
            document.getElementById('loginModal').classList.add('active');
            document.body.style.overflow = 'hidden';
            // Focus the username field when the modal opens
            document.getElementById('username').focus();
        }

        function closeLoginModal() {
            document.getElementById('loginModal').classList.remove('active');
            document.body.style.overflow = 'auto';
        }

        // Close modal when clicking outside
        document.getElementById('loginModal').addEventListener('click', function(e) {
            if (e.target === this) {
                closeLoginModal();
            }
        });

        // Close modal on escape key
        document.addEventListener('keydown', function(e) {
            if (e.key === 'Escape' && document.getElementById('loginModal').classList.contains('active')) {
                closeLoginModal();
            }
        });

        // Smooth scrolling for anchor links
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                const targetId = this.getAttribute('href');
                
                // FIX: Check if the href is just "#" (the Home link), which is an invalid selector
                if (targetId === '#') {
                    window.scrollTo({
                        top: 0,
                        behavior: 'smooth'
                    });
                    return; // Stop processing further for this link
                }

                const target = document.querySelector(targetId);

                if (target) {
                    const offset = targetId === '#features' ? 64 : 0; // Offset for sticky header
                    window.scrollTo({
                        top: target.offsetTop - offset,
                        behavior: 'smooth'
                    });
                }
            });
        });
    </script>
</body>
</html>